<template>
  <el-button type="primary" @click="addUserHandler">添加用户</el-button>
  <!-- 展示已有用户 -->
  <el-table :data="userListRef" style="margin: 20px 0">
    <el-table-column type="index" label="索引" width="100">
      <template #default="scope">
        {{ scope.$index + 1 + sizeRef * (nowPageRef - 1) }}
      </template>
    </el-table-column>
    <el-table-column label="用户名" prop="username"></el-table-column>
    <el-table-column label="昵称" prop="name"></el-table-column>
    <el-table-column label="电话" prop="phone">
      <template #default="scope">
        {{ scope.row.phone ? scope.row.phone : "(空)" }}
      </template>
    </el-table-column>
    <el-table-column label="角色" prop="role_id">
      <template #default="scope">
        {{ roleList.find((n) => n._id === scope.row.role_id)?.name }}
      </template>
    </el-table-column>
    <el-table-column label="操作">
      <template #default="scope">
        <el-button
          type="primary"
          size="small"
          @click="updateUserHandler(scope.row)"
          >修改</el-button
        >
        <el-popconfirm
          title="确定要删除此用户么？"
          confirm-button-text="确定"
          cancel-button-text="取消"
          @confirm="delUser(scope.row._id)"
        >
          <template #reference>
            <el-button type="danger" size="small">删除</el-button>
          </template>
        </el-popconfirm>
      </template>
    </el-table-column>
  </el-table>
  <!-- 翻页组件 -->
  <el-pagination
    v-model:current-page="nowPageRef"
    v-model:page-size="sizeRef"
    layout="total, sizes, prev, pager, next, jumper"
    :total="totalRef"
  >
  </el-pagination>
  <!-- 添加/修改用户的弹窗 -->
  <el-dialog :title="titleRef" v-model="userDialog" width="400" @close="close">
    <el-form
      :model="userRef"
      :rules="userRules"
      label-width="auto"
      ref="userFormEle"
    >
      <el-form-item label="用户名" prop="username">
        <el-input v-model="userRef.username"></el-input>
      </el-form-item>
      <el-form-item label="昵称" prop="name">
        <el-input v-model="userRef.name"></el-input>
      </el-form-item>
      <el-form-item label="电话" prop="phone">
        <el-input v-model="userRef.phone"></el-input>
      </el-form-item>
      <el-form-item label="角色" prop="role_id">
        <el-select v-model="userRef.role_id">
          <!-- 动态渲染已有的角色 -->
          <el-option
            v-for="role in roleList"
            :label="role.name"
            :value="role._id"
          ></el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button type="primary" @click="addAndUPdate">确定</el-button>
      <el-button @click="close">取消</el-button>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import { useRoleStore } from "@/store/useRoleStore"
import { storeToRefs } from "pinia"
import { useShowUser } from "./useShowUser"
import { useUserForm } from "./useUserForm"
import { reqDelUser } from "@/api/reqUser"
import { ElMessage } from "element-plus"
const roleStore = useRoleStore()
const { roleList } = storeToRefs(roleStore)

const { userListRef, nowPageRef, sizeRef, totalRef, getUserByPage } =
  useShowUser()

// 模块划分，哪些是公共的，那些是添加/修改的
const {
  close,
  userDialog,
  userFormEle,
  userRef,
  titleRef,
  userRules,
  addUserHandler,
  updateUserHandler,
  addAndUPdate,
} = useUserForm(getUserByPage, nowPageRef, sizeRef)
// -----------------公共数据截至--------------------

// --------------------添加用户的逻辑-----------------------
// 删除用户
const delUser = async (userId: string) => {
  const { status, msg } = await reqDelUser(userId)
  if (status === 0) {
    ElMessage.success(msg)
    // 重新加载页面
    totalRef.value--
    // getUserByPage
  } else {
    ElMessage.error(msg)
  }
}
</script>

<style></style>
